package com.kzj.laimeimei.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kzj.laimeimei.R;
import com.kzj.laimeimei.beans.CityBean;
import com.kzj.laimeimei.global.App;
import com.kzj.laimeimei.global.Constant;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListView;
import com.kzj.laimeimei.recyclerCard.card.other.CityCard;
import com.kzj.laimeimei.request.BaseResponse;
import com.kzj.laimeimei.request.ICallBackForRequestServer;
import com.kzj.laimeimei.request.RequestServerManager;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * Created by xyg on 2015/11/3.
 */
public class CityChooseActivity extends GBaseActivity {
    private TextView textView_gps;
    private MaterialListView material_listview;
    public static final String TAG_ADDRESS = "TAG_ADDRESS";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.activity_city);
        findViewByIds();
        initDatas();
        TaskGetUserInfo();
    }

    @Override
    protected void findViewByIds() {
        textView_gps = (TextView) findViewById(R.id.textView_gps);
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
        material_listview.setColumnLayout(3);
    }

    @Override
    protected void initDatas() {
//        String address=getIntent().getStringExtra(TAG_ADDRESS);
        String address = App.getInstance().getLocationCity();
        if (!TextUtils.isEmpty(address)) {
            textView_gps.setText(address);
        }
        setTopTitle("选择城市");
        setLeftImg(View.VISIBLE, R.drawable.back);
    }

    @Override
    protected void fillArrayInListView() {

    }


    protected void fillArrayInListView(List<CityBean> cityBeanList) {
        for (int i = 0; i < cityBeanList.size(); i++) {
            CityCard cityCard = new CityCard(this);
            cityCard.setCityBean(cityBeanList.get(i));
            material_listview.add(cityCard);
        }

    }

    private void TaskGetUserInfo() {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_config_city, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_config_city), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        List<CityBean> cityBeanList = JSON.parseArray(JSON.toJSONString(result.getRetval()), CityBean.class);
                        fillArrayInListView(cityBeanList);
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
